<template>
	<view class="content">
		<view class="user-info">
			<view class="topView">
				<image class="setIcon" src="../../static/universal_back_trans.png" mode="widthFix" @click="toSetting"></image>
				<view class="lab2" @click="btnChangeEorC">
				   {{language}}
				</view>
			</view>
			<image src="../../static/logo1.png" mode="widthFix" class="logo"></image>
			<view class="lab1">{{i18n.helloMan}}</view>
			<view class="InputV">
				<view class="inputIphone">
					<image src="../../static/loginIphone.png" mode="widthFix"></image>
					<input type="number" :placeholder="i18n.mobile" :value="iphoneText" maxlength="11" @input='changePhone' @focus='btnStrartClick'/>
				    <view class="labCode" @click="btnCodeClick">{{countdown}}<text v-show="disabled">{{i18n.retry}}</text></view>
				</view>
				<view class="inputIphone  inputPsw">
					<image src="../../static/loginCode.png" mode="widthFix"></image>
					<input type="number"  :placeholder="i18n.inputCode" :value="pswText" maxlength="6" @input='changePsw'  />
				</view>
				<!-- <view class="checkG">
					<label class="radio">
						<radio value="checkboxBool" :checked="checkboxBool" @click='btnChangeRadio' />
					</label>
					<view class="lab3">
						{{i18n.showPolicy}}<text @click="btnPolicyClick">{{i18n.policy}}</text>{{i18n.and}}<text @click="btnPolicyClick">{{i18n.agreement}}</text>
					</view>
				</view> -->
				
				<view class="btn" @click="btnLoginClick">{{i18n.login}}</view>
			</view>
			<image class="imgBottom" src="../../static/loginBottom.png" mode="widthFix"></image>
		</view>
		
		
	</view>
</template>

<script>
	
	import {phoneLogin,getSmsCode,wxLogin} from '../../api/index.js'
	export default {
		data() {
			return {
				checkboxBool:false,
				lab2:'',
				language:'',
				placeho:'',
				iphoneText:'',
				countdown:'',
				pswText:'',
				unionId:'',
				clear:'',
				msgDict:{},
				disabled:false
				
				
				
			}
		},
		computed: {
		  i18n() {
		    return getApp().globalData.$t('loginOne')
		  }
		},
		onLoad(options) {
			
			if (options.unionId) {
				
			this.unionId = options.unionId
				
			}
			
			let currentLang = uni.getStorageSync('CURRENT_LANG')
			if (currentLang === 'en') {
			  this.language = '中文'
			} else {
			  this.language = 'English'
			}
			
			let msgDict = getApp().globalData.$t('loginOne');
			this.msgDict = msgDict;
			this.countdown = msgDict.code
			uni.getStorage({
				key:'userName',
				success: (resInfo) => {
					
					this.iphoneText = resInfo.data
				}
			})

		},
		methods: {
			
			//发送验证码
			async NewsendSms(){
				// console.log('手机号码'+this.iphoneText)
			  	try{
					
					let {data} = await getSmsCode({phone:this.iphoneText});
					uni.showToast({
						title:this.msgDict.smsSendSuccess,
						icon:'none'
					})
					this.getCode();
					
				} catch(err) {
					// console.log('我请求啦',err);
					uni.showToast({
						title:err.message,
						icon:'none'
					})
				}
			},
			//发送验证码
			async newWxLogin(dict){
				// console.log('手机号码'+this.iphoneText)
			  	try{
					
					let {data} = await wxLogin(dict);
					// uni.showToast({
					// 	title:this.msgDict.smsSendSuccess,
					// 	icon:'none'
					// })
					// this.getCode();
					
				} catch(err) {
					// console.log('我请求啦',err);
					uni.showToast({
						title:err.message,
						icon:'none'
					})
				}
			},
			//验证验证码并登陆
			async NewcheckCode(dict){
				console.log(dict)
			      try{
					let {result} = await phoneLogin(dict);
					
				
					uni.setStorage({
					key:'token',
					data:result.token,
					})
					uni.setStorage({
						key:'userId',
						data:result.userId
					})
					
					if(result.bingdingStudent == '0'){
						getApp().globalData.needAddChild = true;
						uni.setStorage({
							key:'needAddChild',
							data:'0'
						})
						// needAddChild要改
					}else{
						getApp().globalData.needAddChild = false;
						uni.setStorage({
							key:'needAddChild',
							data:'1'
						})
						getApp().globalData.appSchoolId = result.schoolList[0].schoolId
						
						uni.setStorage({
							key:'appSchoolId',
							data:result.schoolList[0].schoolId
						})
						getApp().globalData.stuId = result.schoolList[0].stuId
						uni.setStorage({
							key:'stuId',
							data:result.schoolList[0].stuId
						})
					}
					
					// #ifdef APP-PLUS
					this.bindAliasAndTag(this.iphoneText,[]);
					// #endif
					uni.setStorage({
						key:'userName',
						data:result.account
					})
				
					getApp().globalData.token=result.token;
					getApp().globalData.userId=result.userId;
					getApp().globalData.AppLogin=true;
					
					uni.showToast({
						icon:'success',
						title:this.msgDict.logSuccess
					})
					setTimeout(()=>{
						uni.switchTab({
							url:'../index/index'
						})
					},400)
					
					
					
					
					
			      } catch(err) {
					// console.log('我请求啦',err);
					uni.showToast({
						title:err.msg,
						icon:'none'
					})
				   }
			},
			btnLoginClick(){
				// console.log('登录');
			
				let iphone = this.iphoneText;
				let code = this.pswText;
				if (iphone == '') {
					
				 uni.showToast({
				 	title:this.msgDict.inputIphone,
					icon:'none'
				 })
				 
				}else if (! this.validatePhone(iphone)) {
					uni.showToast({
						title:this.msgDict.inIphoneError,
						 icon:'none'
					})
				} else if (code == ''){
					
					uni.showToast({
						title:this.msgDict.inputCode,
						icon:'none'
					})
					
		        // } else if (this.checkboxBool == false){
		        	
		        // 	uni.showToast({
		        // 		title:this.msgDict.checkbox,
		        // 		icon:'none'
		        // 	})
								
				}else{
					
					this.NewcheckCode({smscode:code,phone:iphone,unionId:this.unionId});
				}
			
			},
			
			toSetting(){
				uni.navigateBack({
					delta:1
				})
			},
			bindAliasAndTag(alias, tags) {
			        var isAndorid, PushManager, context, Instance, GeTuiSdk;
			 
			        if (plus.os.name == 'Android') {
			            isAndorid = true;
			        } else {
			            isAndorid = false;
			        }
			 
			        if (isAndorid) {
			            PushManager = plus.android.importClass("com.igexin.sdk.PushManager");
			            context = plus.android.runtimeMainActivity().getContext();
			            Instance = PushManager.getInstance();
			        } else {
			            GeTuiSdk = plus.ios.importClass("GeTuiSdk");
			        }
			 
			        if (isAndorid) {
			//             console.log('获取到cid==', Instance.getClientid(context));
			//             console.log('获取到版本号==', Instance.getVersion(context));
			//             console.log('获取到tags==', JSON.stringify(tags));
			            console.log('获取到alias==',alias);
			 
			            //绑定别名
			            Instance.bindAlias(context, alias);
			 
			            //绑定标签
			            /*
			            var Tag = plus.android.importClass("com.igexin.sdk.Tag");
			            var tagParam = new Array();
			            for (let i = 0; i < tags.length; i++) {
			                var tag = new Tag();
			                tag.setName(tags[i]);
			                tagParam[i] = tag;
			 
			                console.log('tags[' + i + ']==', tags[i]);
			            }
			            console.log('tagParam==', JSON.stringify(tagParam));
			            Instance.setTag(context, tagParam, Date.now() + "");
			           */
			
			        } else {
			            GeTuiSdk.bindAliasandSequenceNum(alias, alias);
			//             GeTuiSdk.setTags(tags);
			        }
			    },
			btnCodeClick(){
				let iphone = this.iphoneText;
				if ( iphone == '') {
					uni.showToast({
						icon:'none',
						title:this.msgDict.mobile
						
					})
					return
					
				}else if (! this.validatePhone(iphone)) {
					uni.showToast({
						title:this.msgDict.inIphoneError,
						 icon:'none'
					})
				    return
				}else{
					this.NewsendSms();
				}
			},
			getCode(){

				this.countdown='59';
				this.disabled=true;
				this.clear = setInterval(()=>{
					this.countDown();
				},1000);
			},
			countDown(){
				if (this.countdown<=1) {
					this.disabled = false;
					this.countdown = this.msgDict.inputCode;
					clearInterval(this.clear);
				} else{
					this.countdown--;
				}
			},
			reload(){
				if(this.disabled){
					this.disabled = false;
					this.countdown = this.msgDict.inputCode;
					clearInterval(this.clear);
				}
			},
			changePhone(e){
				this.iphoneText = e.detail.value;
			},
			changePsw(e){
				this.pswText=e.detail.value;

			},
			btnStrartClick(){
				this.placeho=''
				this.lab2=this.msgDict.mobile
			},
			validatePhone (phone) {
			  const reg = /^(1[3-9])[0-9]{9}$/;
			  return reg.test(phone)
			},
			btnChangeEorC(){
				
				if (this.language = 'English') {
					
					uni.showToast({
						icon:'loading',
						title:'努力切换中'
					},1500)
					
					setTimeout(()=>{
						this.language = '中文'
						getApp().globalData._i18n.locale = 'en'
						uni.setStorageSync('CURRENT_LANG', 'en')
						this.countdown = 'code'
					},1500)
					
					
				} else{
					
					uni.showToast({
						icon:'loading',
						title:'Try to switch'
					},1500)
					
					setTimeout(()=>{
						getApp().globalData._i18n.locale = 'zh-CN'
						uni.setStorageSync('CURRENT_LANG', 'zh-CN')
						this.countdown = '验证码'
						this.language = 'English'
					},1500)
					
					
				}
				let msgDict = getApp().globalData.$t('loginOne');
				this.msgDict = msgDict;
				
				
			},
			btnChangeRadio(event){
				// console.log('event',event);
				this.checkboxBool = !this.checkboxBool
			},
			btnPolicyClick(){
				
				uni.navigateTo({
					url:'../policyInfo/policyInfo'
				})
				
			}
			
		}
	}
</script>

<style lang="less">
.user-info {
		// width: 100%;
		// height: 150rpx;
		// background-color: #fff;
		// padding: 20rpx;
		background: linear-gradient(90deg, #8250FD 0%, #5644E5 100%);
		height: 580rpx;
		.topView{
			padding:0 20rpx;
			/* #ifdef APP-PLUS */
		    padding-top: calc(22upx + var(--status-bar-height));
			/* #endif */
			/*#ifdef MP-WEIXIN */
			padding-top: calc(22upx + var(--status-bar-height));
			/* #endif */
			/*#ifdef H5 */
			padding-top: calc(28upx + var(--status-bar-height));
			/* #endif */
			display: flex;
			justify-content: space-between;
			
			.setIcon{
				
				width: 60rpx;
				height: 60rpx;
			}
			.lab2{
				display: flex;
				
				align-items: center;
				font-size: 28upx;
				color: #fff;
				
			}
			
			
		}
	}
	.logo{
		width: 104rpx;
		height: 104rpx;
		border-radius: 16rpx;
		margin-left:323rpx;
		margin-top: 40rpx;
	}
	.lab1{
		text-align: center;
		font-size: 38rpx;
		margin-top: 30rpx;
		
	}
	.InputV{
		margin-top: 80rpx;
		padding: 75rpx;
		background-color: #fff;
		border-radius: 70rpx 70rpx 0px 0px;
		.inputIphone{
			// border-bottom: 1rpx solid #f2f2f2;
			background: #F7F5FF;
			border-radius: 20rpx;
			// padding-bottom: 20rpx;
			display: flex;
			align-items: center;
			height: 96rpx;
			
			image{
				width: 32rpx;
				height: 36rpx;
				margin-left: 20rpx;
				margin-right: 20rpx;
			}
			.labIphone{
				flex: 0 0 1;
				
				height: 40rpx;
				padding-bottom: 20rpx;
				font-size: 28rpx;
				color: #333;
			}
			.labCode{
				width: 150rpx;
				text-align: center;
				font-size: 28upx;
				font-weight: 400;
				color: #007aff;
				white-space: nowrap;
			}
		}
		
	}
	.inputPsw{
		// margin-top: 30upx;
		// display: flex;
		// justify-content: space-between;
		// border-bottom: 1rpx solid #f2f2f2;
		// padding-bottom: 20rpx;
		margin-top: 40rpx;
		
	}
	
	.checkG{
		display: flex;
		margin-top: 30upx;
		align-items: center;
		
	}
	.checkG .radio{
		margin-right: 10rpx;
	}
	.lab3{
	
		font-size: 26rpx;
		color: #888;
	}
	.lab3 text{
		padding-left: 10rpx;
		color: #1E90FF;
	}
	.btn{
	   color: #fff;
	   background:linear-gradient(135deg, #2D63F9 0%, #6421D3 100%);
	   box-shadow: 0px 2px 24px 0px rgba(80, 58, 225, 0.36);
	   height: 84rpx;
	   line-height: 84rpx;
	   border-radius: 42rpx;
	   // margin-top: 30rpx;
	   margin: 120rpx 20rpx 0rpx;
	   font-size: 32rpx;
	   text-align: center;
	   
   }
   
   .imgBottom{
	   width: 750rpx;
	   
	   position: fixed;
	   bottom: 0;
	   left: 0;
   }
</style>

